package com.woohua.demo.controller;

import com.woohua.demo.thread.task.AsyncTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CountDownLatch;

/***
 * @title DoctorController
 * @description
 * @author woo hua
 * @version 1.0.0
 * @create 2023/2/24 8:49
 **/
@Slf4j
@RestController
@RequestMapping("/completeFeature")
public class CompleteFeatureController {

    @Autowired
    private AsyncTask asyncTask;

    @GetMapping("/threadTest")
    public String threadTest() {
        // 任务总数
        final int taskCount = 5;
        // 单次计数器
        CountDownLatch countDownLatch = new CountDownLatch(taskCount);
        for (int i = 0; i < taskCount; i++) {
            try {
                asyncTask.doTask1(i, countDownLatch);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        log.info("All tasks finished.");

        // 阻塞等待线程池任务执行完
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 线程池执行完
        System.out.println();
        System.out.println("线程池任务执行完成！");
        return "success";
    }

}
